package com.example.speedwaymanager;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;

import java.util.ArrayList;

@SuppressLint("NewApi")
public class SingleMatchSelectorActivity extends Activity implements
		OnItemSelectedListener {

    private CGameManager _gameManager = CGameManager.getInstance();
    private CHomeTeam _teamHome;
    private CAwayTeam _teamAway;
    private boolean _homeTeamChecked;
	private boolean _awayTeamChecked;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_single_match_selector);
		// Show the Up button in the action bar.
		// Make sure we're running on Honeycomb or higher to use ActionBar APIs
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
		UIinit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_single_match_selector, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void UIinit() {
		Spinner spinner1 = (Spinner) findViewById(R.id.spinner1_change);
		Spinner spinner2 = (Spinner) findViewById(R.id.spinner2_change);
		spinner1.setOnItemSelectedListener(this);
		spinner2.setOnItemSelectedListener(this);
		// Create an ArrayAdapter using the string array and a default spinner
		// layout
		ArrayAdapter<CTeam> adapter = new ArrayAdapter<CTeam>(this,
				android.R.layout.simple_spinner_item,
				_gameManager.get_teamsList());
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinner1.setAdapter(adapter);
		spinner2.setAdapter(adapter);
		spinner1.setSelection(1);
		spinner2.setSelection(2);
	}

	/** Called when the user touches the button */
	public void startSingleMatch(View view) {
		// Do something in response to button click
		ArrayList<CTeam> teams = new ArrayList<CTeam>();
		teams.add(_teamHome);
		teams.add(_teamAway);
		_gameManager.set_selectedTeams(teams);	
		_gameManager.startSingleMatch(teams);
		// start new Home screen
		Intent intent = new Intent(this, TableActivity.class);
		startActivity(intent);
	}

	/** Called when the user touches the button */
	public void changeTeams(View view) {
		Spinner spinner1 = (Spinner) findViewById(R.id.spinner1_change);
		Spinner spinner2 = (Spinner) findViewById(R.id.spinner2_change);
		int pos1 = spinner1.getSelectedItemPosition();
		spinner1.setSelection(spinner2.getSelectedItemPosition());
		spinner2.setSelection(pos1);
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int pos,
			long id) {
		// An item was selected. You can retrieve the selected item using
		CTeam team = (CTeam) parent.getItemAtPosition(pos);
		switch (parent.getId()) {
		case R.id.spinner1_change:
            _teamHome = new CHomeTeam(team);
            break;
            case R.id.spinner2_change:
                _teamAway = new CAwayTeam(team);
                break;
        }
	}

	public void checkBoxClicked(View view) {
		// Is the view now checked?
		Button homeButton = (Button) findViewById(R.id.button_homeSquad);
		Button awayButton = (Button) findViewById(R.id.button_awaySquad);
		boolean checked = ((CheckBox) view).isChecked();
		CheckBox chk = ((CheckBox) view);
		if (checked) {
			chk.setText(getString(R.string.user_checkbox));
		} else {
			chk.setText(getString(R.string.cpu_checkbox));
		}
		switch (view.getId()) {
		case R.id.checkBox1:			
			homeButton.setEnabled(checked);
			_teamHome.set_isUser(checked);
			break;
		case R.id.checkBox2:
			awayButton.setEnabled(checked);	
			_teamAway.set_isUser(checked);
			break;
		}
	}
	
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
	}

	public void selectHomeSquad(View view) {
		_gameManager.set_selectedSquadTeam(_teamHome);
		Intent intent = new Intent(this, SquadActivity.class);
		startActivity(intent);
	}
	
	public void selectAwaySquad(View view) {
		_gameManager.set_selectedSquadTeam(_teamAway);
		Intent intent = new Intent(this, SquadActivity.class);
		startActivity(intent);
	}
}
